<template>
  <view class="form" :style="{
      '--global-text-font-size': globalTextFontSize + 'px',
      '--page-height': pageHeight,
    }" :class="['theme-'+theme,model==='PC'?'pc-model-page':'']">
    <view>
      <a-form :class="{'pc-model':model==='PC'}" v-if="colsV2Data && isArray(fields)&&fields.length>0" :fields="fields"
        :pageType="srvType" :formType="use_type" ref="bxForm" @value-blur="valueChange" :mainData="defaultVal"
        :srvApp="appName">
      </a-form>
    </view>
    <view class="button-box" v-if="isArray(fields) && fields.length > 0&&!disabled">
      <button class="cu-btn bg-orange round lg bx-bg-color" v-for="(btn, btnIndex) in colsV2Data._formButtons"
        :key="btnIndex" @click="onButton(btn)">
        {{ btn.button_name }}
      </button>
    </view>
  </view>
</template>

<script>
  import {
    mapState
  } from 'vuex';
  export default {
    data() {
      return {
        appName: '',
        service: '', //自定义按钮的服务
        serviceName: '',
        srvType: 'add', // 表单信息 add | update  | select |list | detail
        use_type: 'add', // detail | proclist | list | treelist | detaillist | selectlist | addchildlist | updatechildlist | procdetaillist | add | update
        colsV2Data: {
          _fieldInfo: [],
          _formButtons: []
        },
        fields: [],
        hideColumn: [], // 不显示的字段 
        condition: null,
        fieldsCond: [],
        params: {},
        defaultVal: false,
        showChildService: false,
        successTip: '操作成功',
        afterSubmit: 'back', // 提交后的操作 detail-跳转到表单详情，back-返回上一页面,home-返回店铺首页,close:关闭当前页面 仅pc端框架内嵌页面中有用
        submitAction: '', //提交后要进行的emit操作
        keyboardHeight: 0,
        pageHeight: `calc(100vh - var(--window-top) - var(--window-bottom))`,
        disabled: false
      };
    },
    computed: {
      model() {
        return getApp()?.globalData?.systemInfo?.model
      },
      childService() {
        if (this.showChildService !== 'false' && this.showChildService !== false && this.colsV2Data && Array
          .isArray(this.colsV2Data.child_service)) {
          return this.colsV2Data.child_service;
        } else {
          return [];
        }
      },
      ...mapState({
        inviterInfo: state => state.app.inviterInfo,
        storeInfo: state => state.app.storeInfo,
        shareType: state => state.app.shareType,
        userInfo: state => state.user.userInfo,
        doctorInfo: state => state.app.doctorInfo,
        globalTextFontSize: state => state.app.globalTextFontSize,
        globalLabelFontSize: state => state.app.globalLabelFontSize,
        theme: state => state.app.theme
      })
    },
    methods: {
      toChildServiceList(e) {
        let data = this.deepClone(e);
        let formData = this.params.defaultVal;
        let condition = [{
          colName: e.foreign_key.column_name,
          ruleType: 'eq',
          value: formData[e.foreign_key.referenced_column_name]
        }];
        if (e.foreign_key && e.foreign_key.referenced_column_name && formData[e.foreign_key
            .referenced_column_name]) {
          if (e.foreign_key.more_config && e.foreign_key.more_config.targetType) {
            let targetType = e.foreign_key.more_config.targetType;
            if (targetType === 'list') {
              uni.navigateTo({
                url: '/publicPages/list/list?pageType=list&serviceName=' + e.service_name +
                  '&cond=' + JSON.stringify(condition)
              });
            } else if (targetType === 'detail') {
              if (e.childData && e.childData.data && e.childData.data.length > 0) {
                let fieldsCond = [];
                if (e.foreign_key && e.foreign_key.referenced_column_name && e.foreign_key.column_name) {
                  fieldsCond.push({
                    column: id,
                    value: e.childData.data[0].id,
                    display: false
                  });
                }
                let url =
                  `/publicPages/formPage/formPage?type=update&serviceName=${e.service_name}&fieldsCond=${encodeURIComponent(JSON.stringify(fieldsCond))}`;
                uni.navigateTo({
                  url: url
                });
              } else {
                uni.showModal({
                  title: '提示',
                  content: '暂无数据，是否添加数据',
                  success(res) {
                    if (res.confirm) {
                      // referenced_column_name //被引用的字段
                      // column //子表字段
                      let fieldsCond = [];
                      if (e.foreign_key && e.foreign_key.referenced_column_name && e
                        .foreign_key.column_name) {
                        fieldsCond.push({
                          column: e.foreign_key.referenced_column_name,
                          value: formData[e.foreign_key.column_name]
                        });
                      }
                      let url = `/publicPages/formPage/formPage?type=add&serviceName=${e.service_name.replace('_select', '_add')}&fieldsCond=${encodeURIComponent(
                      JSON.stringify(fieldsCond)
                    )}`;
                      uni.navigateTo({
                        url: url
                      });
                    }
                  }
                });
              }
            }
          } else {
            let viewTemp = {};
            let label = []
            if (e.service_name === 'srvhealth_plan_schedule_select') {
              // 方案计划
              viewTemp = {
                title: 'ds_name',
                tip: 'play_srv',
                footer: 'start_date'
              };
            }
            if (e.service_name === 'srvhealth_see_doctor_record_select') {
              // 就诊列表
              viewTemp = {
                title: 'name',
                footer: 'chief_complaint'
              };
            }
            if (e.service_name === 'srvhealth_store_user_select') {
              // 用户列表
              viewTemp = {
                img: 'profile_url',
                title: 'person_name',
                tip: 'sex',
                footer: 'user_role'
              };
            }
            if (e.service_name === 'srvhealth_store_dept_select') {
              // 部门列表
              viewTemp = {
                img: 'photo',
                title: 'introduction',
                tip: 'sex'
              };
            }
            if (e.service_name === 'srvhealth_drugstore_inventory_select') {
              // 药品库存
              viewTemp = {
                img: 'med_packing_pic',
                title: 'medicine_name',
                tip: 'type_tree_no'
              };
            }
            if (e.service_name === 'srvhealth_drug_schedule_doctor_detail_list_select') {
              // 方案计划
              viewTemp = {
                title: 'general_name',
                tip: 'take_times',
                footer: 'remind_time'
              };
            }
            if (e.service_name === 'srvhealth_store_vaccination_appoint_record_select') {
              // 疫苗预约记录
              viewTemp = {
                title: 'customer_name',
                img: 'person_image',
                // footer: 'customer_name'
              };
            }
            if (e.service_name === 'srvhealth_store_vaccination_appointment_select') {
              // 疫苗接种预约列表
              viewTemp = {
                title: 'app_date',
                price: 'app_count',
                footer: 'app_desc'
              };
              label = ['app_date', 'app_count', 'app_desc']
            }
            if (e.service_name === 'srvhealth_store_vaccination_program_select') {
              // 疫苗接种计划列表
              viewTemp = {
                title: 'vaccine_stock_name',
                tip: 'vaccine_type',
                price: 'vaccinate_index'
              };
              label = ['vaccine_stock_name', 'vaccine_type', 'vaccinate_index']
            }
            uni.navigateTo({
              url: '/publicPages/list/list?pageType=list&serviceName=' +
                e.service_name +
                '&label=' +
                JSON.stringify(label) +
                '&cond=' +
                JSON.stringify(condition) +
                '&viewTemp=' +
                JSON.stringify(viewTemp)
            });
          }
        } else {
          uni.navigateTo({
            url: '/publicPages/list/list?pageType=list&serviceName=' + e.service_name + '&cond=' + JSON
              .stringify(condition)
          });
        }
      },
      toPages(type, e) {
        this.srvType = type;
        if (this.params.to && this.params.idCol && this.params.submitData && this.params.submitData[this.params
            .idCol]) {
          uni.redirectTo({
            url: `${this.params.to}?${this.params.idCol}=${this.params.submitData[ this.params.idCol ]}`
          });
        } else {
          let serviceName = e?.service_name || this.colsV2Data?.select_service_name || this.getServiceName(this
            .serviceName)
          let url =
            `/publicPages/formPage/formPage?type=${type}&serviceName=${serviceName}&fieldsCond=${encodeURIComponent(JSON.stringify(this.fieldsCond))}`
          if (type === 'update' || type == 'detail') {
            if (this.params?.submitData?.id) {
              let fieldsCond = [{
                column: 'id',
                value: this.params.submitData.id,
                display: false
              }]
              url =
                `/publicPages/formPage/formPage?type=${type}&serviceName=${serviceName}&fieldsCond=${encodeURIComponent(JSON.stringify(fieldsCond))}`

            }
          }

          if (Array.isArray(this.hideColumn) && this.hideColumn.length > 0) {
            url += `&hideColumn=${JSON.stringify(this.hideColumn)}`
          }

          if (this.appName) {
            url += `&appName=${this.appName}`
          }
          uni.redirectTo({
            url: url
          });
        }
      },
      async selectServiceNo() {
        if (!this.userInfo || !this.userInfo.no) {
          return;
        }
        let serviceName = 'srvhealth_service_record_select';
        let url = this.getServiceUrl('health', serviceName, 'select');
        let req = {
          serviceName: serviceName,
          colNames: ['*'],
          condition: [{
            colName: 'user_info_no',
            ruleType: 'eq',
            value: this.userInfo.no
          }]
        };
        let res = await this.$http.post(url, req);
        if (Array.isArray(res.data.data) && res.data.data.length > 0) {
          return res.data.data[0];
        }
      },
      async updateWeight(weight) {
        let serviceLog = await this.selectServiceNo();
        const serviceName = 'srvhealth_body_fat_measurement_record_add';
        let url = this.getServiceUrl('health', serviceName, 'operate');
        let req = [{
          serviceName: serviceName,
          data: [{
            service_no: serviceLog.no,
            name: serviceLog.name,
            weight: weight
          }]
        }];
        if (weight) {
          this.$http.post(url, req);
        }
      },
      async onButton(e) {
        let self = this;
        if (!e) {
          return;
        }
        if (!this.isOnButton) {
          this.isOnButton = true;
        } else {
          // uni.showToast({
          // 	title: '正在处理中，请勿重复操作',
          // 	icon: 'none'
          // });
          // return;
        }
        let req = this.$refs.bxForm.getFieldModel();
        for (let key in req) {
          if (Array.isArray(req[key])) {
            req[key] = req[key].toString();
          }
        }
        switch (e.button_type) {
          case 'edit':
            if (e.page_type === '详情' && this.use_type === 'detail') {
              this.toPages('update', e);
              this.isOnButton = false;
            } else {
              if (req) {
                if (e.service_name === 'srvhealth_person_info_update' && req.weight) {
                  this.updateWeight(req.weight);
                }
                req = [{
                  serviceName: e.service_name,
                  data: [req],
                  condition: this.condition
                }];
                if (self?.params?.defaultVal?.id) {
                  req[0].condition = [{
                    colName: 'id',
                    ruleType: 'eq',
                    value: self.params.defaultVal.id
                  }];
                }
                let app = self.appName || uni.getStorageSync('activeApp');
                let url = self.getServiceUrl(app, e.service_name, 'add');
                if (!Array.isArray(req[0].condition) || req[0].condition.length === 0) {
                  uni.showToast({
                    title: '参数错误，请刷新重试',
                    icon: 'none'
                  });
                  return;
                }
                let res = await this.onRequest('update', e.service_name, req, app);
                let service = e.service_name.slice(0, e.service_name.lastIndexOf('_'))
                if (res.data.state === 'SUCCESS') {
                  uni.$emit('dataChange', e.service_name)
                  if (
                    Array.isArray(res.data.response) &&
                    res.data.response.length > 0 &&
                    res.data.response[0].response &&
                    Array.isArray(res.data.response[0].response.effect_data) &&
                    res.data.response[0].response.effect_data.length > 0
                  ) {
                    this.params.submitData = res.data.response[0].response.effect_data[0];
                    if (e.service_name === 'srvhealth_person_info_update') {
                      this.$store.commit('SET_USERINFO', this.params.submitData);
                      uni.setStorageSync('cur_user_no', this.params.submitData.no)
                    }
                  }
                  uni.showModal({
                    title: '提示',
                    content: `${res.data.resultMessage}`,
                    showCancel: false,
                    success(res) {
                      if (self.shareType && self.shareType === 'seeDoctor') {
                        // 通过邀请就诊登记链接进入 跳转到就诊信息登记页面
                        let fieldsCond = [{
                          column: 'user_info_no',
                          display: false
                        }, {
                          column: 'user_no',
                          display: false
                        }];
                        if (self.doctorInfo && self.doctorInfo.no) {
                          fieldsCond.push({
                            column: 'doctor_no',
                            display: false,
                            value: self.doctorInfo.no
                          }, {
                            column: 'doctor_name',
                            display: false,
                            value: self.doctorInfo.name
                          });
                          if (self.doctorInfo.store_no) {
                            fieldsCond.push({
                              column: 'store_no',
                              display: false,
                              value: self.doctorInfo.store_no
                            });
                          }
                        }
                        let path =
                          '/publicPages/formPage/formPage?share_type=seeDoctor&serviceName=srvhealth_see_doctor_record_add&type=add&fieldsCond=' +
                          encodeURIComponent(JSON.stringify(fieldsCond));
                        uni.redirectTo({
                          url: path
                        });
                      } else if (self.afterSubmit === 'back') {
                        if (self.submitAction) {
                          uni.$emit(self.submitAction)
                        }
                        uni.navigateBack()
                      } else if (self.afterSubmit === 'home') {
                        let store_no = this.$store?.state?.app?.storeInfo?.store_no
                        uni.reLaunch({
                          url: `/storePages/home/home?store_no=${store_no}`
                        })
                      } else if (self.afterSubmit === 'close') {
                        if (top.window?.tab?.closeCurrentTab && top?.window?.tab?.getCurrentTab) {
                          let curTab = top.window?.tab.getCurrentTab();
                          console.log(curTab, '：-----》curTab')
                          if (curTab?.layId) {
                            top.window?.tab.closeCurrentTab(curTab.layId)
                            return
                          }
                        }
                      } else if (self.afterSubmit === 'detail') {
                        self.toPages('detail');
                        return
                      } else {
                        uni.navigateBack()
                        // self.toPages('detail');
                      }
                    }
                  });
                } else {
                  uni.showToast({
                    title: res.data.resultMessage,
                    mask: false,
                    icon: 'none'
                  });
                }
                this.isOnButton = false;
              }
            }
            break;
          case 'submit':
            if (req) {
              req = this.fields.reduce((res, cur) => {
                res[cur.columns] = cur.value
                return res
              }, {})

              for (let key in req) {
                if (!req[key]) {
                  delete req[key];
                }
              }

              this.params.defaultVal = req;
              let data = this.deepClone(req);
              req = [{
                serviceName: e.service_name,
                data: [data]
              }];
              let app = this.appName || uni.getStorageSync('activeApp');
              let url = this.getServiceUrl(app, e.service_name, 'add');
              let res = await this.$http.post(url, req);
              uni.$emit('dataChange', e.service_name)
              if (res.data.state === 'SUCCESS') {
                if (
                  Array.isArray(res.data.response) &&
                  res.data.response.length > 0 &&
                  res.data.response[0].response &&
                  Array.isArray(res.data.response[0].response.effect_data) &&
                  res.data.response[0].response.effect_data.length > 0
                ) {
                  this.params.submitData = res.data.response[0].response.effect_data[0];
                  if (e.service_name === 'srvhealth_store_mgmt_add') {
                    // 创建店铺成功，自动为其创建圈子,并为新创建的圈子添加管理员（群成员）
                    self.createGroupCircle(this.params.submitData);
                    self.addPersonToStore(this.params.submitData);
                  }
                }
                uni.showModal({
                  title: '提示',
                  content: this.successTip,
                  showCancel: false,
                  success(res) {
                    if (res.confirm) {
                      let beforeRedirectUrl = getApp().globalData.beforeRedirectUrl
                      if (self.afterSubmit === 'home') {
                        getApp().globalData.beforeRedirectUrl = null
                        let store_no = self.$store?.state?.app?.storeInfo?.store_no
                        uni.reLaunch({
                          url: `/storePages/home/home?store_no=${store_no}`
                        })
                        return
                      } else if (self.afterSubmit === 'close') {
                        if (top.window?.tab?.closeCurrentTab && top?.window?.tab?.getCurrentTab) {
                          let curTab = top.window?.tab.getCurrentTab();
                          console.log(curTab, '：-----》curTab')
                          if (curTab) {
                            top.window?.tab.closeCurrentTab(curTab)
                            return
                          }
                        }
                      } else if (beforeRedirectUrl) {
                        uni.redirectTo({
                          url: beforeRedirectUrl
                        })
                        getApp().globalData.beforeRedirectUrl = null
                        return
                      } else if (self.afterSubmit === 'close') {
                        // getApp().globalData.beforeRedirectUrl = null
                        // let store_no = this.$store?.state?.app?.storeInfo?.store_no
                        // uni.reLaunch({
                        //   url: `/storePages/home/home?store_no=${store_no}`
                        // })
                        if (top.window?.tab?.closeCurrentTab && top?.tab?.getCurrentTab) {
                          let curTab = top.tab.getCurrentTab();
                          if (curTab) {
                            top.tab.closeCurrentTab(curTab)
                            return
                          }
                        }
                      } else if (self.afterSubmit === 'detail') {
                        self.toPages('detail');
                        return
                      }
                      if (self.shareType && self.shareType === 'seeDoctor') {
                        // 通过邀请就诊登记链接进入
                        if (self.serviceName === 'srvhealth_see_doctor_record_add') {
                          // 登记成功 跳转到医生信息页面
                          // TODO 医生信息页面
                          console.log(self.doctorInfo);
                          uni.redirectTo({
                            url: `/personalPages/myDoctor/myDoctor`
                          });
                        }
                      } else if (self.afterSubmit === 'back') {
                        if (self.submitAction) {
                          uni.$emit(self.submitAction)
                        }
                        uni.navigateBack()
                      } else {
                        self.toPages('detail');
                      }
                    }
                  }
                });
              } else {
                uni.showToast({
                  title: res.data.resultMessage,
                  mask: false,
                  icon: 'none'
                });
              }
              this.isOnButton = false;
            }
            break;
          case 'reset':
            this.$refs.bxForm.onReset();
            this.isOnButton = false;
            break;
          case 'customize':
            if (e.operate_type === '删除') {
              let data = {
                button: e,
                row: this.defaultVal
              }
              this.onButtonToUrl(data, this.appName).then(res => {
                if (res.state === 'SUCCESS') {
                  uni.$emit('dataChange')
                  uni.showModal({
                    title: '提示',
                    content: "操作成功",
                    showCancel: false,
                    success: (res) => {
                      if (res.confirm) {
                        uni.navigateBack()
                      }
                    }
                  })
                }
              })
              return
            }
            if (e.application && e.operate_service && e.requestData) {
              const url = this.getServiceUrl(e.application, e.operate_service, 'operate');
              const req = [{
                data: [e.requestData],
                serviceName: e.operate_service,
                srvApp: e.application
              }];
              let res = await this.$http.post(url, req);
              if (res.data.state === 'SUCCESS') {
                uni.showModal({
                  title: '提示',
                  content: e.tip_msg ? e.tip_msg : res.data.resultMessage,
                  showCancel: false,
                  success(res) {
                    if (res.confirm) {
                      uni.navigateBack();
                    }
                  }
                });
              } else {
                uni.showToast({
                  title: res.data.resultMessage,
                  mask: false,
                  icon: 'none'
                });
              }
              this.isOnButton = false;
            }
            break;
          default:
            uni.showToast({
              title: e.button_name,
              icon: false
            });
            this.isOnButton = false;
            break;
        }
      },
      createGroupCircle(e) {
        let req = [{
          serviceName: 'srvhealth_group_circle_add',
          data: [{
            name: `【${e.name}】`,
            store_no: e.store_no
          }]
        }];
        this.$fetch('operate', 'srvhealth_group_circle_add', req, 'health').then(res => {
          if (Array.isArray(res.data) && res.data.length > 0) {
            this.addPersonToGroup(res.data[0]);
          }
        });
      },
      addPersonToStore(e) {
        // 创建店铺后将当前用户以管理员身份加入到店铺用户列表
        let req = [{
          serviceName: 'srvhealth_store_user_add',
          condition: [],
          data: [{
            store_no: e.store_no,
            name: e.name,
            type: e.type,
            person_no: this.userInfo.no,
            person_name: this.userInfo.name,
            user_account: this.userInfo.userInfo,
            nick_name: this.userInfo.nick_name,
            profile_url: this.userInfo.profile_url,
            user_image: this.userInfo.user_image,
            sex: this.userInfo.sex,
            user_role: '管理员'
          }]
        }];
        this.$fetch('operate', 'srvhealth_store_user_add', req, 'health');
      },
      addPersonToGroup(e) {
        // 创建了店铺关联的圈子后将创建人以管理员身份加入到这个圈子
        let req = [{
          serviceName: 'srvhealth_person_group_circle_add',
          condition: [],
          data: [{
            person_no: this.userInfo.no,
            user_no: this.userInfo.userno,
            name: this.userInfo.name,
            profile_url: this.userInfo.profile_url,
            user_image: this.userInfo.user_image,
            gc_no: e.gc_no,
            group_role: '管理员'
          }]
        }];
        this.$fetch('operate', 'srvhealth_person_group_circle_add', req, 'health');
      },
      async valueChange(e, triggerField) {
        const column = triggerField.column
        const fieldModel = e
        const cols = this.colsV2Data._fieldInfo.filter(item => item.x_if).map(item => item.column)
        const table_name = this.colsV2Data.main_table
        let calcResult = {}
        let calcCols = this.colsV2Data._fieldInfo.filter(item => item.redundant?.func && Array.isArray(item
          .calc_trigger_col) && item.calc_trigger_col.includes(column)).map(item => item.column)
        if (Array.isArray(calcCols) && calcCols.length > 0) {
          calcResult = await this.evalCalc(table_name, calcCols, fieldModel, this.appName)
        }

        let result = null
        if (Array.isArray(cols) && cols.length > 0) {
          result = await this.evalX_IF(table_name, cols, fieldModel, this.appName)
        }
        for (let i = 0; i < this.fields.length; i++) {
          const item = this.fields[i]
          item.old_value = item.value
          if (calcResult?.response && (calcResult.response[item.column] || calcResult.response[item.column] === 0)) {
            if (item.redundant?.trigger === 'always' || !item.value) {
              item.value = calcResult?.response[item.column]
              fieldModel[item.column] = item.value
            }
          }
          if (item.x_if) {
            if (Array.isArray(item.xif_trigger_col) && item.xif_trigger_col.includes(column)) {
              if (item.table_name !== table_name) {
                result = await this.evalX_IF(item.table_name, [item.column], fieldModel, this.appName)
              }
              if (result?.response && result.response[item.column]) {
                item.display = true
              } else {
                item.display = false
              }
            }
          }
          if (e && typeof e === 'object' && e.hasOwnProperty(item.column)) {
            item.value = e[item.column];
            fieldModel[item.column] = item.value
          }
          this.$set(this.fields, i, item)
          // if (item.old_value !== item.value) {
          //   this.valueChange(fieldModel, item)
          // }
        }
      },
      getServiceName(srv) {
        let len = srv.lastIndexOf('_');
        let serviceName = srv.slice(0, len) + '_';
        if (this.srvType === 'list' || this.srvType === 'detail') {
          serviceName += 'select';
        } else {
          serviceName += this.srvType;
        }
        return serviceName;
      },
      async getDefaultVal() {
        if (this.use_type === 'detail' || this.use_type === 'update') {
          let serviceName = this.colsV2Data?.select_service_name || this.service || this.serviceName.replace(
              '_update',
              '_select')
            .replace('_add', '_select');
          let condition = this.fieldsCond
            .filter(item => item.value)
            .map(item => {
              return {
                colName: item.column,
                ruleType: item.column === 'id' ? "eq" : 'like',
                value: item.value
              };
            });
          if (condition.find(item => item.colName === 'id')) {
            condition = condition.filter(item => item.colName === 'id')
          }
          if (!condition || condition.length === 0) {
            return
          }
          let app = this.appName || uni.getStorageSync('activeApp');
          let url = this.getServiceUrl(app, serviceName, 'select');
          let req = {
            serviceName: serviceName,
            colNames: ['*'],
            condition: this.condition || condition,
            page: {
              pageNo: 1,
              rownumber: 2
            }
          };
          let res = await this.$http.post(url, req);
          if (res.data.state === 'SUCCESS') {
            if (Array.isArray(res.data.data) && res.data.data.length > 0) {
              this.params.defaultVal = res.data.data[0];
              this.defaultVal = res.data.data[0];
              return res.data.data[0];
            }
          }
        }
      },
      async getFieldsV2() {
        let self = this
        let app = this.appName || uni.getStorageSync('activeApp');
        let colVs = await self.getServiceV2(self.serviceName, self.srvType, self.use_type, app);
        let defaultVal = self.defaultVal;
        self.colsV2Data = colVs;
        colVs = self.deepClone(colVs);
        if (colVs && colVs.service_view_name) {
          uni.setNavigationBarTitle({
            title: colVs.service_view_name
          });
        } else {
          if (!colVs) {
            return
          }
        }
        let fields = null
        let modal = colVs._fieldInfo.reduce((res, cur) => {
          if (cur.defaultValue) {
            res[cur.column] = cur.defaultValue
            cur.value = cur.defaultValue
            // if (self.defaultVal && self.defaultVal[cur.column]) {
            //   self.defaultVal[cur.column] = cur.value
            // }
          } else if (cur.value) {
            res[cur.column] = cur.value
          }
          return res
        }, {})
        switch (colVs.use_type) {
          case 'update':
          case 'detail':
            defaultVal = self.defaultVal ? self.defaultVal : await self
              .getDefaultVal();
            if (Array.isArray(defaultVal) && defaultVal.length > 0) {
              defaultVal = defaultVal[0]
            }
            fields = self.setFieldsDefaultVal(colVs._fieldInfo, defaultVal ? defaultVal : self.params
              .defaultVal || {});
            if (!fields) {
              return;
            }
            fields = fields.map(field => {
              if (Array.isArray(field?.option_list_v2?.conditions) && field.option_list_v2
                .conditions
                .length > 0) {
                field.option_list_v2.conditions = this.evalConditions(field.option_list_v2
                  .conditions, this.defaultVal)
              }
              if (field.type === 'Set' && Array.isArray(field.option_list_v2)) {
                field.option_list_v2 = field.option_list_v2.map(item => {
                  item.checked = false;
                  return item;
                });
              }
              if (Array.isArray(self.fieldsCond) && self.fieldsCond.length > 0) {
                self.fieldsCond.forEach(item => {
                  if (item.column === field.column) {
                    if (item.hasOwnProperty('display')) {
                      field.display = item.display;
                    }
                    if (item.hasOwnProperty('value')) {
                      field.value = item.value;
                    }
                    if (field.option_list_v2 && Array.isArray(field.option_list_v2
                        .conditions) && Array.isArray(item.condition)) {
                      field.option_list_v2.conditions = field.option_list_v2
                        .conditions.concat(item.condition);
                    } else if (field.option_list_v2 && !field.option_list_v2
                      .conditions && Array.isArray(item.condition)) {
                      field.option_list_v2.conditions = item.condition;
                    }
                  }
                });
              }
              return field;
            }).filter(item => !self.hideColumn.includes(item.column))
            break;
          case 'add':
            fields = colVs._fieldInfo.map(field => {
              if (field.type === 'Set' && Array.isArray(field.option_list_v2)) {
                field.option_list_v2 = field.option_list_v2.map(item => {
                  item.checked = false;
                  return item;
                });
              }
              // if (Array.isArray(field?.option_list_v2?.conditions) && field.option_list_v2
              //   .conditions
              //   .length > 0) {
              //   field.option_list_v2.conditions = this.evalConditions(field.option_list_v2
              //     .conditions, this.fieldModel)
              // }
              if (self.defaultCondition && Array.isArray(self.defaultCondition) && colVs
                ._fieldInfo && Array.isArray(colVs._fieldInfo)) {
                self.defaultCondition.forEach(cond => {
                  colVs._fieldInfo.forEach(field => {
                    if (cond.colName === field.column && (!['Image'].includes(field.col_type) || (cond[
                        'value'] && cond['value'].indexOf('http') !== -1))) {
                      field['value'] = cond['value'];
                      // field['disabled'] = true;
                    }
                  });
                });
              }
              if (Array.isArray(self.fieldsCond) && self.fieldsCond.length > 0) {
                self.fieldsCond.forEach(item => {
                  if (item.column === field.column) {
                    if (item.hasOwnProperty('display')) {
                      field.display = item.display;
                    }
                    if (item.hasOwnProperty('disabled')) {
                      field.disabled = item.disabled;
                    }
                    if (item.hasOwnProperty('value') && (!['Image'].includes(field.col_type) || (item.value &&
                        item.value.indexOf('http') !== -1))) {
                      // 不复制照片字段
                      field.value = item.value;
                    }
                    if (field.option_list_v2 && Array.isArray(field.option_list_v2
                        .conditions) && Array.isArray(item.condition)) {
                      field.option_list_v2.conditions = field.option_list_v2
                        .conditions.concat(item.condition);
                    } else if (field.option_list_v2 && !field.option_list_v2
                      .conditions && Array.isArray(item.condition)) {
                      field.option_list_v2.conditions = item.condition;
                    }
                  }
                });
              }
              return field;
            }).filter(item => !self.hideColumn.includes(item.column))
            break;
        }

        console.log(this.deepClone(colVs._fieldInfo))
        modal = colVs._fieldInfo.reduce((res, cur) => {
          if (cur.defaultValue && !cur.value) {
            res[cur.column] = cur.defaultValue
            cur.value = cur.defaultValue
            if (self.defaultVal && self.defaultVal[cur.column]) {
              self.defaultVal[cur.column] = cur.value
            }
          } else if (cur.value) {
            res[cur.column] = cur.value
          }
          return res
        }, {})
        const cols = colVs._fieldInfo.filter(item => item.x_if).map(item => item.column)
        const table_name = colVs.main_table
        const result = await this.evalX_IF(table_name, cols, modal, this.appName)

        let calcResult = {}
        let calcCols = colVs._fieldInfo.filter(item => item.redundant?.func && Array.isArray(item.calc_trigger_col))
          .map(item => item.column)

        if (Array.isArray(calcCols) && calcCols.length > 0) {
          calcResult = await this.evalCalc(table_name, calcCols, modal, this.appName)
        }




        for (let i = 0; i < colVs._fieldInfo.length; i++) {
          const item = colVs._fieldInfo[i]

          if (calcResult?.response && (calcResult.response[item.column] || calcResult.response[item.column] == 0)) {

            if (item.redundant?.trigger === 'always' || !item.value) {
              item.value = calcResult?.response[item.column]
              modal[item.column] = item.value
              this.defaultVal[item.column] = item.value
            }
          }


          if (item.x_if) {
            if (Array.isArray(item.xif_trigger_col)) {
              if (item.table_name !== table_name) {
                result = await this.evalX_IF(item.table_name, [item.column], modal, this.appName)
              }
              if (result?.response && result.response[item.column]) {
                item.display = true
              } else if (result?.response && result.response[item.column] == false) {

                item.display = false
              }
            }
          }
        }
        this.fields = fields
      },

      async getStoreUserInfo(no) {
        let url = this.getServiceUrl('health', 'srvhealth_store_user_select', 'select');
        let req = {
          serviceName: 'srvhealth_store_user_select',
          colNames: ['*'],
          condition: [{
            colName: 'person_no',
            ruleType: 'eq',
            value: this.userInfo.no
          }, {
            colName: 'store_no',
            ruleType: 'eq',
            value: no
          }]
        };
        let res = await this.$http.post(url, req);
        if (this.vstoreUser?.store_no === no) {
          this.storeUserInfo = res.data.data[0];
          return [this.vstoreUser]
        }
        if (Array.isArray(res.data.data) && res.data.data.length > 0) {
          this.storeUserInfo = res.data.data[0];
          return res.data.data;
        }
      },
      async getStoreInfo(no) {
        let url = this.getServiceUrl('health', 'srvhealth_store_mgmt_select', 'select');
        let req = {
          serviceName: 'srvhealth_store_mgmt_select',
          colNames: ['*'],
          condition: [{
            colName: 'store_no',
            ruleType: 'eq',
            value: no
          }]
        };
        let res = await this.$http.post(url, req);
        if (Array.isArray(res.data.data) && res.data.data.length > 0) {
          this.storeInfo = res.data.data[0];
          return res.data.data;
        }
      },
      async addToStore(store_no, invite_no, storeInfo = {}) {
        // 添加用户到单位
        let url = this.getServiceUrl('health', 'srvhealth_store_user_add', 'operate');
        let req = [{
          serviceName: 'srvhealth_store_user_add',
          condition: [],
          data: [{
            name: storeInfo.name,
            person_no: this.userInfo.no,
            type: storeInfo.type,
            user_account: this.userInfo.userno,
            user_role: '用户',
            add_url: this.inviterInfo.add_url,
            invite_user_no: this.inviterInfo.invite_user_no,
            store_no: store_no
          }]
        }];
        let res = await this.$http.post(url, req);
        if (res.data.state === 'SUCCESS') {
          uni.showModal({
            title: '提示',
            content: `您已成功加入【${storeInfo.name}】`
          });
        }
      },
      async handlePreAction(action) {
        if (action?.actionNo) {
          let arr = action.actionNo.split(',')
          if (arr.length > 0 && Array.isArray(action?.valMap) && action.valMap.length == arr.length) {
            if (action?.type === 'syncAssign') {
              // 同步赋值
              let valueArr = []
              let self = this
              let fieldsCond = []
              for (let i = 0; i < arr.length; i++) {
                let req = {
                  "serviceName": "srvsys_page_def_select",
                  "colNames": ["*"],
                  "condition": [{
                    colName: "pt_no",
                    ruleType: "eq",
                    value: arr[i]
                  }],
                  "page": {
                    "pageNo": 1,
                    "rownumber": 1
                  }
                }
                let app = self.appName || uni.getStorageSync('activeApp');
                let url = self.getServiceUrl('health', 'srvsys_page_def_select', 'select');
                let resp = await self.$http.post(url, req)
                if (Array.isArray(resp?.data?.data) && resp.data.data.length > 0) {
                  let obj = resp.data.data[0];
                  if (obj.service && obj.service_json) {
                    let req1 = null;
                    try {
                      req1 = JSON.parse(obj.service_json);
                      console.log(req1)
                    } catch (e) {
                      //TODO handle the exception
                    }
                    console.log(typeof req1)
                    if (typeof req1 === 'object') {
                      let url1 = self.getServiceUrl(obj?.app || app, obj.service, 'select');
                      let res1 = await self.$http.post(url1, req1)
                      console.log(res1)
                      if (Array.isArray(res1?.data?.data) && res1.data.data.length > 0) {
                        valueArr.push(res1.data.data[0])
                      }
                    }
                  }
                }
              }

              if (valueArr.length >= action.valMap.length) {
                action.valMap.forEach((val, index) => {
                  if (typeof val === 'object') {
                    Object.keys(val).forEach(key => {
                      let obj = {}
                      obj.column = key;
                      try {
                        obj.value = valueArr[index][val[key]]
                      } catch (e) {
                        //TODO handle the exception
                      }

                      if (obj.value || obj.value === null || obj.value === 0) {
                        obj.disabled = true;
                        fieldsCond.push(obj)
                      }
                    })
                  }
                })
              }
              return {
                fieldsCond
              }
            }
          }
        }
      },

    },
    onHide() {
      uni.offKeyboardHeightChange(function() {})
    },
    beforeDestroy() {
      uni.offKeyboardHeightChange(function() {})
    },
    async onLoad(option) {
      if (option.disabled) {
        this.disabled = option.disabled
      }
      uni.onKeyboardHeightChange(res => {
        console.log(res.height)
        if (res.height) {
          this.pageHeight =
            `calc(100vh - ${res.height}px - var(--window-top) - var(--window-bottom))`
        } else {
          this.pageHeight = `calc(100vh - var(--window-top) - var(--window-bottom))`
        }
        this.keyboardHeight = res.height
      })
      await this.toAddPage();
      this.checkOptionParams(option);
      const destApp = option.destApp;
      if (destApp) {
        this.appName = option.destApp
        // uni.setStorageSync('activeApp', destApp);
      }
      if (option.appName) {
        this.appName = option.appName
      }
      if (option.hideColumn) {
        this.hideColumn = JSON.parse(option.hideColumn)
      }
      if (option.afterSubmit) {
        this.afterSubmit = option.afterSubmit
      }
      if (option.submitAction) {
        this.submitAction = option.submitAction
      }
      if (option.showChildService) {
        this.showChildService = option.showChildService;
      }
      if (option.params) {
        option.params = JSON.parse(decodeURIComponent(option.params));
        if (option.params?.defaultVal) {
          if (Array.isArray(option.params.defaultVal) && option.params.defaultVal.length > 0) {
            option.params.defaultVal = option.params.defaultVal[0]
          }
          this.defaultVal = option.params.defaultVal
        }
        this.params = option.params
      }
      if (option.condition) {
        try {
          let condition = JSON.parse(decodeURIComponent(option.condition))
          this.condition = condition.filter(item => {
            if (item.value == null || item.value == undefined) {
              if (item.ruleType === 'eq') {
                return false
              }
            }
            return true
          })
        } catch (e) {
          //TODO handle the exception
        }
      }
      if (option.service) {
        this.service = option.service
      }
      if (option.successTip) {
        this.successTip = option.successTip
      }
      if (option.serviceName) {
        this.serviceName = option.serviceName;
      }
      // srvType: 'add', // 表单信息 add | update  | select |list | detail
      // use_type: 'add', // detail | proclist | list | treelist | detaillist | selectlist | addchildlist | updatechildlist | procdetaillist | add | update
      if (option.type) {
        if (option.type.indexOf(',') !== -1) {
          option.type = option.type.split(',')[0];
        }
        if (option.type === 'form') {
          option.type = 'add';
        }
        this.srvType = option.type;
        this.use_type = option.type;
      }
      if (option.fieldsCond) {
        let fieldsCond = null
        try {
          let reg = /\\/g;
          option.fieldsCond = option.fieldsCond.replace(reg, '')
        } catch (e) {
          //TODO handle the exception
        }
        try {
          fieldsCond = JSON.parse(option.fieldsCond);

        } catch (e) {
          console.warn(e);
          try {
            fieldsCond = JSON.parse(decodeURIComponent(option.fieldsCond));
          } catch (err) {
            //TODO handle the exception
            console.warn(err);
          }
        }
        if (option.fill_batch_no && Array.isArray(fieldsCond)) {
          fieldsCond.push({
            column: "fill_batch_no",
            display: false,
            value: option.fill_batch_no
          })
        }
        if (Array.isArray(fieldsCond) && fieldsCond.length > 0) {
          if (option.serviceName === 'srvhealth_see_doctor_record_add') {
            if (this.doctorInfo && this.doctorInfo.no) {
              console.log(this.doctorInfo);
              this.getStoreUserInfo(this.doctorInfo.store_no).then(res => {
                this.getStoreInfo(this.doctorInfo.store_no).then(data => {
                  if (!Array.isArray(res) || res.length === 0) {
                    // 当前用户不在此诊所中 则添加当前用户到此诊所中
                    if (option.store_no && option.invite_user_no) {
                      if (Array.isArray(data) && data.length > 0) {
                        this.addToStore(option.store_no, option
                          .invite_user_no, data[0]);
                      } else {
                        this.addToStore(option.store_no, option
                          .invite_user_no);
                      }
                    } else {
                      if (this.doctorInfo.store_no) {
                        this.addToStore(this.doctorInfo.store_no);
                      }
                    }
                  }
                });
              });
              fieldsCond.push({
                column: 'doctor_no',
                value: this.doctorInfo.no,
                display: false
              });
              fieldsCond.push({
                column: 'doctor_name',
                display: false
              });
            }
          }
          this.fieldsCond = fieldsCond.map(item => {
            if (option.serviceName.indexOf('srvhealth_see_doctor') === -1) {
              if (item.column === 'user_info_no' && this.userInfo.no && !item.value) {
                item.value = this.userInfo.no;
              }
              if (item.column === 'user_no' && this.userInfo.userno && !item.value) {
                item.value = this.userInfo.userno;
              }
              if (item.column === 'store_no' && this.doctorInfo.store_no && !item.value) {
                item.value = this.doctorInfo.store_no;
                item.display = false;
              }
              if (item.value && typeof item.value === 'string' && item.value.indexOf('${')) {
                item.value = this.renderStr(item.value, this)
              }
            }
            return item;
          });
        }
      }
      if (option.preAction) {
        let action = option.preAction
        try {
          action = JSON.parse(option.preAction)
        } catch (e) {
          //TODO handle the exception
          console.log(e)
        }
        let data = await this.handlePreAction(action)
        if (Array.isArray(data?.fieldsCond) && data.fieldsCond.length > 0) {
          this.fieldsCond = [...this.fieldsCond, ...data.fieldsCond]
        }
      }
      if (this.type === 'detail' && (!this.fieldsCond || (Array.isArray(this.fieldsCond) && this.fieldsCond.length ===
          0)) && option.id) {
        this.fieldsCond = [{
          column: 'id',
          ruleType: 'eq',
          value: option.id
        }]
      }
      await this.getFieldsV2();
      await this.getDefaultVal();
    }
  };
</script>

<style lang="scss" scoped>
  // page{
  // 	background-color: #fff;
  // }
  .form {
    width: 100%;
    // background-color: #f2f3f4;
    padding: 20rpx;
    background-color: #F8F8FA;
    min-height: var(--page-height);
    /* #ifdef MP-WEIXIN */
    height: 100vh;
    /* #endif */
    overflow-y: scroll;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    .form-wrapper{
      min-height: var(--page-height);
      height: 100vh;
    }
    .scroll-view {
      // background-color: #fff;
    }

    &.pc-model-page {
      background-color: #fff;
      max-width: 800px;
      margin: 0 auto;
      padding: 0 50px;
    }

    .pc-model {
      display: flex;
      flex-wrap: wrap;
      padding-top: 10px;
      padding-left: 10px;

      ::v-deep .form-item::after {
        border-bottom: none !important;
      }

      ::v-deep .field-item {
        min-width: 100% !important;
        width: 100% !important;
        border-radius: 0 !important;

        // border: none!important;
        .form-item-content {
          // border: 1px solid #f1f1f1;
        }
      }
    }

    .button-box {
      margin: 0 auto;
      margin-top: 20rpx;
      margin-bottom: 0;
      width: 100%;
      min-height: 120rpx;
      display: flex;
      padding: 40rpx 20rpx;
      justify-content: space-around;
      align-items: center;
      // z-index: 20;
      max-width: 500px;

      .cu-btn {
        min-width: 45%;
      }
    }
  }
  

</style>
